<?php	
class Obj_Object {

	protected static $t;

	public static function init() {
		self::$t = new Obj_DBTable_Objects();
	}

	public static function getObjects($city) {
		$ar_ = self::$t->getObjects($city);
		$ar = array();
		foreach ($ar_ as $k => $v) {
			$ar[$v['ID']] = $v;
		}
		return $ar;
	}
	
	public static function getAllPaginatedObjects($page, $active_only = false) {
		return self::$t->getAllPaginatedObjects($page, $active_only);
	}
	
	public static function getAllPaginatedObjectsByCondition($page, $condition, $active_only = false) {
		return self::$t->getAllPaginatedObjectsByCondition($page, $condition, $active_only);
	}
	
	public static function getAllObjects() {
		$ar_ = self::$t->getAllObjects();
		$ar = array();
		foreach ($ar_ as $k => $v) {
			$ar[$v['ID']] = $v;
		}
		return $ar;
	}

	public static function getFrontpageObjects() {
		return self::$t->getFrontPageObjects();
	}

	public static function getFriendObjects($id) {
		return self::$t->getFriendObjects($id);
	}

	public static function getObjectLink($ar) {
		$s = HTTP_HOST.'/';
		$s .= $ar['CountryAlias'].'/';
		$s .= $ar['RegionAlias'].'/';
		$s .= $ar['CityAlias'].'/';
		$s .= $ar['LocationAlias'].'/';
		$s .= $ar['Alias'].'/id'.$ar['ID'];
		return $s;
	}

	public static function saveObject($arData, $id) {
		if ($id == 0) {
			return self::$t->insert($arData);
		}
		else self::$t->update($arData, 'ID = '.intval($id));
		return intval($id);
	}

	public static function getObject($id, $activeOnly = false) {
		return self::$t->getObject($id, $activeOnly);
	}

	public static function findObject($alias, $activeOnly = false) {
		return self::$t->findObject($alias, $activeOnly);
	}

	public static function delObject($id){
		return self::$t->delete('ID = '.intval($id));
	}

	public static function getImageObjectPath($id){
		(string)$specid = str_pad(substr((string)$id, 0, 3) , 3, "0", STR_PAD_LEFT);
		$path = (string)OBJECT_IMAGE_FOLDER."/".$specid."/".$id."/";		
		if(is_dir($path)){
			return $path;
		} else {		
			if(!is_dir(OBJECT_IMAGE_FOLDER))chdir(OBJECT_IMAGE_FOLDER);
			chdir(OBJECT_IMAGE_FOLDER);
	    	if(!is_dir($specid))mkdir($specid, 0777); chdir($specid);
	    	if(!is_dir($id))mkdir($id, 0777); chdir($id);				
			return $path;
		}
	}
	
	public static function getObjectYears() {
		$ar_ = self::$t->getObjectYears();
		$ar = array();
		foreach ($ar_ as $k => $v) {
			$ar[$v['year']] = array('Name' => $v['year']);
		}
		return $ar;
	}
	
	public static function getSitemapObjects($location) {
		return self::$t->getSitemapObjects($location);
	}
	
	public static function checkAlias($country, $region, $city, $location, $alias) {
		$i = 0;
		$al = $alias; 
		while (self::$t->checkAlias($country, $region, $city, $location, $al)) {
			$al = $alias.'_'.$i++;
		}
		return $al;
	}
	
	public static function getLink($id) {
		$row = self::$t->getLinkRow($id);
		if (!$row) return false;
		$link = '/'.implode('/', array_reverse($row)).'/id'.$id;
		//$link = '/'.implode('/', array_reverse($row));
		return $link;
	}
	
} Obj_Object::init();

